﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TheServer.Models
{
    public class FriendDAO
    {
        public static List<AccountDTO> GetFriend(string acc, string pass, bool openid)
        {
            try
            {
                List<AccountDTO> kq = new List<AccountDTO>();
                var _acc = getAccount(acc, pass, openid);
                _acc.TinhTrang = true;
                if (openid == true)
                    _acc.OpenID = true;
                MyDatabase.db.SaveChanges();
                List<Friend> fr = MyDatabase.db.Friends.ToList();
                for (int i = 0; i < fr.Count; i++)
                {
                    if (fr.ElementAt(i).TaiKhoan == _acc.TaiKhoan)
                    {
                        string tkBan = fr.ElementAt(i).TaiKhoanBan;
                        var friend = MyDatabase.db.Accounts.First(ms => ms.TaiKhoan == tkBan);
                        var accDto = new AccountDTO();
                        accDto.TaiKhoan = friend.TaiKhoan;
                        accDto.MatKhau = friend.MatKhau;
                        accDto.TinhTrang = friend.TinhTrang.Value;
                        accDto.OpenID = friend.OpenID.Value;
                        kq.Add(accDto);
                    }
                }
                return kq;
            }
            catch
            {
                return null;
            }
        }

        private static Account getAccount(string acc, string pass, bool openid)
        {
            if (openid == false)
                return MyDatabase.db.Accounts.First(ms => ms.TaiKhoan == acc && ms.MatKhau == pass);
            else
            {
                bool flag = false;
                foreach (Account _acc in MyDatabase.db.Accounts.ToList())
                {
                    if (string.Compare(_acc.TaiKhoan, acc) == 0)
                    {
                        flag = true;
                        break;
                    }
                }
                if (flag == true)
                    return MyDatabase.db.Accounts.First(ms => ms.TaiKhoan == acc);
                else { 
                    AccountDTO acc_dto = new AccountDTO();
                    acc_dto.TaiKhoan = acc;
                    AccountDAO.SignUpOpenID("signup", acc_dto);
                    return MyDatabase.db.Accounts.First(ms => ms.TaiKhoan == acc);
                }
            }
        }
        public static List<AccountDTO> GetStatusFriends(string acc)
        {
            try
            {
                List<AccountDTO> kq = new List<AccountDTO>();
                var _acc = MyDatabase.db.Accounts.First(ms => ms.TaiKhoan == acc);
                //_acc.TinhTrang = true;
                //MyDatabase.db.SaveChanges();
                List<Friend> fr = MyDatabase.db.Friends.ToList();
                for (int i = 0; i < fr.Count; i++)
                {
                    if (fr.ElementAt(i).TaiKhoan == _acc.TaiKhoan)
                    {
                        string tkBan = fr.ElementAt(i).TaiKhoanBan;
                        var friend = MyDatabase.db.Accounts.First(ms => ms.TaiKhoan == tkBan);
                        var accDto = new AccountDTO();
                        accDto.TaiKhoan = friend.TaiKhoan;
                        accDto.MatKhau = friend.MatKhau;
                        accDto.TinhTrang = friend.TinhTrang.Value;
                        kq.Add(accDto);
                    }
                }
                return kq;
            }
            catch
            {
                return null;
            }
        }

        public static List<AccountDTO> GetStatusFriendsReverse(string acc)
        {
            try
            {
                List<AccountDTO> kq = new List<AccountDTO>();
                var _acc = MyDatabase.db.Accounts.First(ms => ms.TaiKhoan == acc);
                //_acc.TinhTrang = true;
                //MyDatabase.db.SaveChanges();
                List<Friend> fr = MyDatabase.db.Friends.ToList();
                for (int i = 0; i < fr.Count; i++)
                {
                    if (fr.ElementAt(i).TaiKhoanBan == _acc.TaiKhoan)
                    {
                        string tk = fr.ElementAt(i).TaiKhoan;
                        var friend = MyDatabase.db.Accounts.First(ms => ms.TaiKhoan == tk);
                        var accDto = new AccountDTO();
                        accDto.TaiKhoan = friend.TaiKhoan;
                        accDto.MatKhau = friend.MatKhau;
                        accDto.TinhTrang = friend.TinhTrang.Value;
                        kq.Add(accDto);
                    }
                }
                return kq;
            }
            catch
            {
                return null;
            }
        }

        public static List<AccountDTO> AddFriend(FriendDTO myFriend)
        {
            try
            {
                List<AccountDTO> kq = new List<AccountDTO>();
                List<Friend> fr = MyDatabase.db.Friends.ToList();
                int id = 0;

                var friend = MyDatabase.db.Accounts.First(ms => ms.TaiKhoan == myFriend.TaiKhoanBan);

                try
                {
                    var check = MyDatabase.db.Friends.First(f => f.TaiKhoan == myFriend.TaiKhoan && f.TaiKhoanBan == myFriend.TaiKhoanBan);

                    if (fr.Count == 0)
                        id = 0;
                    else
                        id = fr.Last().Id + 1;
                }
                catch
                {
                    var addFriend = new Friend();
                    addFriend.TaiKhoan = myFriend.TaiKhoan.ToLower();
                    addFriend.TaiKhoanBan = myFriend.TaiKhoanBan.ToLower();
                    if (fr.Count == 0)
                        addFriend.Id = 0;
                    else
                        addFriend.Id = fr.Last().Id + 1;
                    id = addFriend.Id + 1;
                    MyDatabase.db.AddToFriends(addFriend);
                }

                try
                {
                    var check = MyDatabase.db.Friends.First(f => f.TaiKhoan == myFriend.TaiKhoanBan && f.TaiKhoanBan == myFriend.TaiKhoan);
                }
                catch
                {
                    var addBackFriend = new Friend();
                    addBackFriend.TaiKhoan = myFriend.TaiKhoanBan.ToLower();
                    addBackFriend.TaiKhoanBan = myFriend.TaiKhoan.ToLower();
                    addBackFriend.Id = id;
                    MyDatabase.db.AddToFriends(addBackFriend);
                }

                MyDatabase.db.SaveChanges();
                List<Friend> fr2 = MyDatabase.db.Friends.ToList();

                for (int i = 0; i < fr2.Count; i++)
                {
                    if (fr2.ElementAt(i).TaiKhoan == myFriend.TaiKhoan)
                    {
                        string tkBan = fr2.ElementAt(i).TaiKhoanBan;
                        var frnd = MyDatabase.db.Accounts.First(ms => ms.TaiKhoan == tkBan);
                        var accDto = new AccountDTO();
                        accDto.TaiKhoan = frnd.TaiKhoan;
                        accDto.MatKhau = frnd.MatKhau;
                        accDto.TinhTrang = frnd.TinhTrang.Value;
                        kq.Add(accDto);
                    }
                }
                return kq;
            }
            catch
            {
                return null;
            }
        }

        public static List<AccountDTO> DeleteFriend(FriendDTO friendXoa)
        {
            try
            {
                var _acc = MyDatabase.db.Friends.First(ms => ms.TaiKhoan == friendXoa.TaiKhoan && ms.TaiKhoanBan == friendXoa.TaiKhoanBan);
                //   return 2;
                MyDatabase.db.Friends.DeleteObject(_acc);
                MyDatabase.db.SaveChanges();

                List<AccountDTO> kq = new List<AccountDTO>();
                List<Friend> fr = MyDatabase.db.Friends.ToList();
                for (int i = 0; i < fr.Count; i++)
                {
                    if (fr.ElementAt(i).TaiKhoan == friendXoa.TaiKhoan)
                    {
                        string tkBan = fr.ElementAt(i).TaiKhoanBan;
                        var friend = MyDatabase.db.Accounts.First(ms => ms.TaiKhoan == tkBan);
                        var accDto = new AccountDTO();
                        accDto.TaiKhoan = friend.TaiKhoan;
                        accDto.MatKhau = friend.MatKhau;
                        accDto.TinhTrang = friend.TinhTrang.Value;
                        kq.Add(accDto);
                    }
                }
                return kq;
            }
            catch
            {
                return null;
            }
        }
    }
}